package bishe;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

public class admintable {
	public JPanel read() throws IOException {
		JPanel pan = new JPanel();
		pan.setLayout(null);
		mydata data = new mydata(admindata.getreaddata().size(), 4, admindata.getreaddata());
		// 设置组件
		JButton add = new JButton("上架");
		JButton romave = new JButton("下架");
		JTable table = new JTable(data);
		JScrollPane sp = new JScrollPane(table);
		// 设定按键位置
		add.setBounds(254, 5, 100, 20);
		romave.setBounds(354, 5, 100, 20);
		sp.setBounds(20, 30, 690, 595);
		// 添加监听器
		romave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO 自动生成的方法存根
				int count = table.getSelectedRow();// 获取你选中的行号（记录）
				String getname = table.getValueAt(count, 1).toString();
				try {
					Goods.remove(getname);
					mydata data = new mydata(admindata.getreaddata().size(), 4, admindata.getreaddata());
					table.setModel(data);
					JOptionPane.showMessageDialog(pan, "移除成功");
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
		});
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				JFrame f = new JFrame("商品上架");
				f.setLayout(new BorderLayout());
				f.setSize(400, 400);
				f.setLocation(300, 200);
				JPanel pan = new JPanel();
				pan.setLayout(null);
				JLabel[] a = new JLabel[4];
				JTextField[] b = new JTextField[4];
				// 标签初始化
				a[0] = new JLabel("商品编号");
				a[0].setBounds(20, 35, 150, 20);
				a[1] = new JLabel("商品名称");
				a[1].setBounds(20, 85, 260, 20);
				a[2] = new JLabel("商品规格");
				a[2].setBounds(20, 135, 100, 20);
				a[3] = new JLabel("商品价格");
				a[3].setBounds(20, 185, 100, 20);
				// 文本框初始化
				b[0] = new JTextField(25);
				b[0].setBounds(80, 35, 260, 20);
				b[1] = new JTextField(25);
				b[1].setBounds(80, 85, 260, 20);
				b[2] = new JTextField(25);
				b[2].setBounds(80, 135, 260, 20);
				b[3] = new JTextField(25);
				b[3].setBounds(80, 185, 260, 20);
				// 添加确定按钮
				JButton ton = new JButton("确定");
				ton.setBounds(160, 250, 100, 30);
				pan.add(a[0]);
				pan.add(a[1]);
				pan.add(a[2]);
				pan.add(a[3]);
				pan.add(b[0]);
				pan.add(b[1]);
				pan.add(b[2]);
				pan.add(b[3]);
				pan.add(ton);
				f.add(pan);
				f.setUndecorated(false);
				f.setVisible(true);
				ton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO 自动生成的方法存根
						try {
							if ((b[0].getText() + b[1].getText() + b[2].getText() + b[3].getText()).length() <= 3) {
								JOptionPane.showMessageDialog(f, "内容不能为空");
							} else {
								Goods.add(b[0].getText(), b[1].getText(), b[2].getText(), b[3].getText());
								mydata data = new mydata(admindata.getreaddata().size(), 4, admindata.getreaddata());
								table.setModel(data);
								f.dispose();
							}
						} catch (IOException e1) {
							// TODO 自动生成的 catch 块
							e1.printStackTrace();
						}
					}
				});
			}
		});
		pan.add(add);
		pan.add(romave);
		pan.add(sp);
		return pan;
	}

	public JPanel found() throws IOException {
		JPanel pan = new JPanel();
		pan.setLayout(null);
		pan.setPreferredSize(new Dimension(300, 60));
		JLabel bel = new JLabel("商品名称");
		bel.setBounds(165, 5, 80, 20);
		JTextField txt = new JTextField(20);
		txt.setBounds(255, 5, 200, 20);
		JButton ton = new JButton("确认查询");
		ton.setBounds(490, 5, 90, 20);
		pan.add(txt);
		pan.add(ton);
		pan.add(bel);
		mydata data = new mydata(admindata.getfounddata().size(), 4, admindata.getfounddata());
		// 全局表格3 查询
		JTable table3 = new JTable(data);
		JScrollPane scro = new JScrollPane(table3);
		scro.setBounds(20, 30, 690, 595);
		ton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					if (txt.getText().length() == 0) {
						JOptionPane.showMessageDialog(pan, "名称不能为空");
						// 名称输入框获取焦点
						txt.grabFocus();
						return;
					} else {
						String str;
						BufferedWriter out = new BufferedWriter(new FileWriter("foundtemp.txt"));
						BufferedReader in = new BufferedReader(new FileReader("Goods.txt"));
						while ((str = in.readLine()) != null) {
							if (str.matches("(.*)" + txt.getText() + "(.*)")) {
								out.append(str);
								out.newLine();
							}
						}
						in.close();
						out.close();
						mydata data = new mydata(getdata.getfounddata().size(), 4, getdata.getfounddata());
						table3.setModel(data);
					}
				} catch (IOException e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
			}
		});
		pan.add(scro);
		return pan;
	}

}

//数据模型
class mydata extends AbstractTableModel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6459276977444428210L;
	int rowIndex, columnIndex;
	ArrayList<String> list;
	String[] name = { "id", "name", "size", "price" };

	public mydata() {

	}

	public mydata(int rowindex, int col, ArrayList<String> list) {
		this.rowIndex = rowindex;
		this.columnIndex = col;
		this.list = list;
	}

	@Override
	public int getColumnCount() {
		// TODO 自动生成的方法存根
		return columnIndex;
	}

	@Override
	public String getColumnName(int columnIndex) {
		// TODO 自动生成的方法存根
		return name[columnIndex];
	}

	@Override
	public int getRowCount() {
		// TODO 自动生成的方法存根
		return rowIndex;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		// TODO 自动生成的方法存根
		String[] st = list.get(rowIndex).split(" ");
		if (columnIndex == 0) {
			return st[0];
		} else if (columnIndex == 1) {
			return st[1];
		} else if (columnIndex == 2) {
			return st[2];
		} else
			return st[3];
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		// TODO 自动生成的方法存根

		return false;
	}
}

//模型数据来源
class admindata {
	public static ArrayList<String> getreaddata() throws IOException {
		String str;
		ArrayList<String> list = new ArrayList<String>();
		BufferedReader in = new BufferedReader(new FileReader("Goods.txt"));
		while ((str = in.readLine()) != null) {
			list.add(str);
		}
		in.close();
		return list;
	}

	public static ArrayList<String> getfounddata() throws IOException {
		ArrayList<String> list = new ArrayList<String>();
		String str = null;
		BufferedReader in = new BufferedReader(new FileReader("foundtemp.txt"));
		while ((str = in.readLine()) != null) {
			list.add(str);
		}
		in.close();
		return list;
	}

	public static ArrayList<String> getgoodsdata() {
		ArrayList<String> list = new ArrayList<String>();
		String str = null;
		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader("GoodsList.txt"));

			while ((str = in.readLine()) != null) {
				list.add(str);
			}
			in.close();
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return list;

	}
}
